package eproject.mm.com.projectsupervision.util;

import java.io.IOException;

import eproject.mm.com.projectsupervision.PrjApplication;
import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lsk on 2017/3/9.
 */

public class HttpUtil {
    public static OkHttpClient okHttpClient=new OkHttpClient();
    public static String get(String url){
        Request.Builder builder=new Request.Builder();
        builder.addHeader("authenticationToken",PrjApplication.getInstance().getAuthenticationToken());
        Request request=builder.get().url("http://"+ PrjApplication.getInstance().getServerIp() +":"+PrjApplication.getInstance().getServerPort()+"/"+PrjApplication.getInstance().getPreFixPath()+url).build();

        Call call=okHttpClient.newCall(request);
        //同步GET请求
        try {
            Response response=call.execute();
//            if(!response.isSuccessful()) {
//                Headers responseHeaders = response.headers();
//                for (int i = 0; i < responseHeaders.size(); i++) {
//                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
//                }
//            }
            return response.body().string();
        }catch (Exception e){
            e.printStackTrace();
        }

        //异步GET请求
        /*call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                str=response.body().toString();
            }
        });*/
        return null;
    }
}